// pages/shop_add/shop_add_detail/shop_add_detail.js
const api = require('../../../utils/util.js');
const service = require('../../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [''], //--商品详情数组
    next: 0, //控制初始页面点击标志
    temList: ['-1'], //控制商品详情数量
    add_tag: 0, //是否显示插入模态框
    current_inds: 0, //当前插入指针
  },

  next: function() {
    this.setData({
      next: 1
    })
  },

  ins_el: function(e) {
    let inds = e.currentTarget.dataset.inds;

    if (inds == -1) {
      inds = this.data.temList.length
    }

    this.setData({
      current_inds: inds,
      add_tag: 1
    })

  },

  up_el: function(e) {
    let inds = e.currentTarget.dataset.inds;
    let temList = this.data.temList;
    let data = this.data.data;

    let currentData = data[inds];
    data[inds] = data[inds - 1];
    data[inds - 1] = currentData;

    this.setData({
      data: data
    })
  },

  down_el: function(e) {
    let inds = e.currentTarget.dataset.inds;
    let temList = this.data.temList;
    let data = this.data.data;

    let currentData = data[inds];
    data[inds] = data[inds + 1];
    data[inds + 1] = currentData;

    this.setData({
      data: data
    })
  },

  del_el: function(e) {
    let inds = e.currentTarget.dataset.inds;
    let temList = this.data.temList;
    let data = this.data.data;

    temList.splice(inds, 1);
    data.splice(inds, 1);

    this.setData({
      temList: temList,
      data: data
    })
  },

  //输入事件
  setInput: function(e) {
    let inds = e.currentTarget.dataset.inds;
    let data = this.data.data;

    data[inds] = e.detail.value;

    this.setData({
      data: data
    })
    console.log(this.data.data);
  },

  //插入文本框
  giveTextFrame: function() {
    let inds = this.data.current_inds + parseInt(1);
    let temList = this.data.temList;
    let data = this.data.data;

    temList.splice(inds, 0, '-1');
    data.splice(inds, 0, '');

    this.setData({
      data: data,
      temList: temList
    })
  },

  //插入图片
  givePicture: function() {
    let _this = this;
    let inds = this.data.current_inds + parseInt(1);
    let data = this.data.data.splice();
    let temList = this.data.temList.splice();

    wx.showActionSheet({
      itemList: ['拍照模式', '手机相册'],
      success: function(res) {
        if (res.tapIndex == 0) {
          api.upLoadImg(1, 'camera', function(res) {
            console.log(res)

            temList.splice(inds, 0, 1);
            data.splice(inds, 0, res);


            _this.setData({
              data: data,
              temList: temList
            })
          })
        } else if (res.tapIndex == 1) {
          api.upLoadImg(9, 'album', function(res) {
            for (let i = 0; i < res.length; i++) {
              data.splice(inds, 0, res[i]);
              temList.splice(inds, 0, 1);
              inds++;
            }

            _this.setData({
              data: data,
              current_inds: inds,
              temList: temList
            })
          })
        }
      }
    })
  },

  disappear: function() {
    this.setData({
      add_tag: 0
    })
  },

  save: function() {
    let _this = this;
    let data = this.data.data.slice();
    let arr = [];
    let arrData = [];

    for (let i = 0; i < data.length; i++) {
      if (data[i].indexOf("http://mpcard.oss-cn-shenzhen.aliyuncs.com/") == -1) {
        if (data[i] != '') {
          arr[i] = "<p>" + data[i] + "</p>";
          arrData.push(data[i]);
        }
      } else {
        arr[i] = "<img src='" + data[i] + "'/>";
        arrData.push(data[i]);
      }
    }
    let all_str = arr.join('');

    wx.navigateBack({
      success: function() {
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1]; //当前页面
        var prevPage = pages[pages.length - 2]; //上一个页面

        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
          ['dataObj.description']: all_str,
          goodsDescriptionData: arrData
        })
        currPage.setData({
          ['dataObj.description']: all_str,
          goodsDescriptionData: arrData
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.goodsDescriptionData != '') {
      let goodsDescriptionData = options.goodsDescriptionData.split(',');
      let temList = [];

      for (let i = 0; i < goodsDescriptionData.length; i++) {
        if (goodsDescriptionData[i].indexOf("http://mpcard.oss-cn-shenzhen.aliyuncs.com/") == -1 && goodsDescriptionData[i] != '') {
          temList.push('-1');
        } else {
          temList.push('1');
        }
      }

      this.setData({
        next: 1,
        data: goodsDescriptionData,
        temList: temList
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})